package com.token.sysflow.impl;

import com.token.sql.IntegrateSql;
import com.token.sysflow.core.*;
import com.token.sysflow.enumerate.ActionTypeEnum;
import com.token.sysflow.enumerate.ApprovalTypeEnum;
import com.token.sysflow.exception.ErrorCodeEnum;
import com.token.sysflow.exception.FlowException;
import com.token.sysflow.hander.ApproveProcess;
import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.Objects;

/**
 * 提交流程
 */

public class SubmitFlowImpl extends AbstractFlowHandlerImpl {

    /**
     * 办理人
     */
    @Setter
    @Getter
    private String[] selectApprover;

    /**
     * 首次提交时需要传入:流程Id
     */
    @Setter
    @Getter
    private String flowId;

    /**
     * 首次提交时需要传入:环节Id
     */
    @Setter
    @Getter
    private String stepId;

    /**
     * 首次提交时需要传入:申请人
     */
    @Setter
    @Getter
    private String applyer;

    /**
     * 首次提交时需要传入:创建人
     */
    @Setter
    @Getter
    private String creater;

    /**
     * 首次提交时需要传入：应用KeyId
     */
    @Setter
    @Getter
    private String appKeyId;

    /**
     * 子流程subkeyunid
     */
    @Setter
    @Getter
    private String subKeyUnid;

    /**
     * true 首次提交。
     */
    @Setter
    @Getter
    private boolean isFirstSubmit = false;

    /**
     * 会审提交结果，通过或不通过。
     */
    @Setter
    @Getter
    private int HSResult;

    public static int HSResult_YES = 1;
    public static int HSResult_NO = 0;

    /**
     * 初始化参数
     *
     * @param iSql 类型
     * @throws Exception 抛出错误
     */
    public void initParameter(IntegrateSql iSql) throws Exception {
        if (isFirstSubmit) { //首次提交时。
            flowCore = new SubmitFlow(iSql);
        } else {
            ApproveProcess process = new ApproveProcess(iSql, keyUnid);
            if (!process.isInitRs()) {
                throw new FlowException(ErrorCodeEnum.KEYUNID_NOFIND_ERROR);
            }

//            if(process.getFlowType() == FlowTypeEnum.MAIN_FLOW.getId()) {
//                System.out.println("--->主流程提交");
//                flowCore = new SubmitMain(iSql, process);
//            }else if(process.getFlowType() == FlowTypeEnum.SUB_FLOW.getId()){
//                System.out.println("--->子流程提交");
//                if (subKeyUnid == null || "".equals(subKeyUnid)) {
//                    throw new FlowException(ErrorCodeEnum.FLOWSUBKEYUNID_NOFIND);
//                }
//                flowCore = new SubmitSub(iSql, new ApproveProcess(iSql, subKeyUnid), process);
//            }else{
//
//            }

            //根据提交类型来获取对应的处理类
            ApprovalTypeEnum type = ApprovalTypeEnum.getTypeByValue(process.getApprovalType());
            switch (Objects.requireNonNull(type)) {
                case SP: // 普通提交
                    System.out.println("--->普通提交：" + this.getActionUser() +"-->"+ Arrays.toString(this.getSelectApprover()));
                    flowCore = new SubmitFlow(iSql, process);
                    break;
                case HS:
                    System.out.println("--->会审提交：" + this.getActionUser() +"-->"+ Arrays.toString(this.getSelectApprover()));
                    flowCore = new SubmitHS(iSql, process);
                    break;
                case HQ:
                    System.out.println("--->会签提交：" + this.getActionUser() +"-->"+ Arrays.toString(this.getSelectApprover()));
                    flowCore = new SubmitHQ(iSql, process);
                    break;
                case MZLC:
                    System.out.println("--->子流程提交：" + this.getActionUser() +"-->"+ Arrays.toString(this.getSelectApprover()));
                    if (subKeyUnid == null || "".equals(subKeyUnid)) {
                        throw new FlowException(ErrorCodeEnum.FLOWSUBKEYUNID_NOFIND);
                    }

                    flowCore = new SubmitSub(iSql, new ApproveProcess(iSql, subKeyUnid), process);
                    break;
                case ZZHQ:
                    System.out.println("--->组织会签提交 = " + Arrays.toString(this.getSelectApprover()));
                    flowCore = new SubmitCallHQ(iSql, process);
                    break;
                case ZZHS:
                    System.out.println("--->组织会审提交 = " + Arrays.toString(this.getSelectApprover()));
                    flowCore = new SubmitCallHS(iSql, process);
                    break;
                default:
                    throw new FlowException(ErrorCodeEnum.TASKHQ_APPROVERTYPE);
            }
        }

    }

    /**
     * 提交操作类型
     *
     * @return int
     */
    public ActionTypeEnum getActionType() {
        return ActionTypeEnum.SUBMIT;
    }
}